<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Test extends Controller
{
	public function action_add_user()
	{
		$info = array(
			'displayname' => 'Loc Nguyen',
			'email'		  => 'locto03@yahoo.com',
		);
		$ret = Model::factory('user')->add_user($info);
		echo debug::dump($ret);
	}
	public function action_add_project()
	{
		$ret = Model::factory('project')->add_project(1,'Test project', 'Just a simple project');
		echo Debug::dump($ret);
	}
	public function action_add_task()
	{
		$info['assign_user_id'] = 1;
		$info['user_id']		= 1;
		$info['name']			= 'test';
		$info['deadline']		= '2012-01-17';
		$info['priority']		= 111;
		$ret = Model::factory('task')->add_task($info);
		echo Debug::dump($ret);
	}
	public function action_test_crawler()
	{
		/*
	    if(!$fp = fopen("http://www.nhommua.com/tp-ho-chi-minh/du-lich-gia-tot-hom-nay.html" ,"r" )) 
	    {
	        return false;
	    } //our fopen is right, so let's go
	    $content = "";
	    while(!feof($fp)) { //while it is not the last line, we will add the current line to our $content
	        $content .= fgets($fp, 1024);
	    }
	    fclose($fp); //we are done here, don't need the main source anymore
		//echo $content;
		*/	
		require_once './lib/simple_html_dom.php';	
		$html = file_get_html('http://www.nhommua.com/tp-ho-chi-minh/du-lich-gia-tot-hom-nay.html');

		// Find all images 
		//foreach($html->find('img') as $element) 
		 //     echo $element->src . '<br>';
		
		// Find all links 
		//
		$myFile = "testFile.txt";
		$fh = fopen($myFile, 'w') or die("can't open file");
		$pre_href = array();
		//
		foreach($html->find('a') as $element )
		{
			$href = $element->href;
			if (strpos($href,'deals') != false && strpos($href, 'www.nhommua.com') && !in_array($href, $pre_href)) 
		    //   echo $element->href . '<br>';
			{
				//echo $pre_href .'<br>';
				echo $href .'<br>';
				//echo $element->href . '<br>';
				$pattern = "/^(http:\/\/)?([^\/]+)/i";
		
				preg_match($pattern,
				    $href, $matches);
				$host = $matches[2];
				
				// get last two segments of host name
				preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
				if ($matches[0] == "nhommua.com")
				{
					
					$child_html = file_get_html($href);
						//$this->get_child_info($child_html);
					/*foreach($child_html->find('div[class=detail-deal-info] p span') as $e)
					{
						
						$price = $e->plaintext;
						fwrite($fh, $price.'\n');
						//echo $str;
					}*/
					$t = $child_html->find('div[id=DealInfo] h2 strong');
					$price = 'name: '.$t[0]->plaintext;
					fwrite($fh, $price.'\n');
					
					$t = $child_html->find('div[id=divIMG] a[id=aimgIndexBig1] img', 0);
					$price = 'Image: '.$t->src;
					fwrite($fh, $price.'\n');
					
					
					$t = $child_html->find('div[class=detail-deal-info] p', 0);
					$price = 'discount: '.substr($t->plaintext,0, -4);
					fwrite($fh, $price.'\n');
					
					$t = $child_html->find('div[class=detail-deal-info] p span');
					$price = $t[0]->plaintext;
					fwrite($fh, $price.'\n');
				}
				//echo "domain name is: {$matches[0]}" . '<br>'; ;
				
			}
			$pre_href[] = $href;
		}
		fclose($fh);
	}
	public function action_get_user_info()
	{
		$ret = Model::factory('user')->get_user_info(1);
		echo Debug::dump($ret);	
	}
	public function action_match()
	{
		$test = 'http://www.nhommua.com/tp-ho-chi-minh/deals/tour-nha-trang-3n3d-7F0201070D7C.html';
		$pattern = "/^(http:\/\/)?([^\/]+)/i";
		
		preg_match($pattern,
		    $test, $matches);
		$host = $matches[2];
		
		// get last two segments of host name
		preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
		echo "domain name is: {$matches[0]}\n";
		
	}
	public function action_get_child_info($child_html)
	{
		require_once './lib/simple_html_dom.php';	
		$child_html = file_get_html('http://www.nhommua.com/tp-ho-chi-minh/deals/tour-nha-trang-3n3d-7F0201070D7C.html');
			//$this->get_child_info($child_html);
		foreach($child_html->find('div[class=detail-deal-info] p span') as $e)
		{
			//echo (1);
			$str = $e->plaintext;
			echo $str;
		}
		
		
	
	}
}

?>